package cate.game.rank.cross.h;

import cate.common.table.d.GDRank;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.rank.cross.CrossRankMessageModule;
import cate.game.rank.msg.GuildRankDataResp;
import cate.game.rank.po.GuildRank;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import cate.game.role.res.guild.po.GuildPO;
import cate.game.role.res.guild.po.basic.GuildBasicPO;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GuildCrossRankLoader {
    @Getter
    int rankId;
    /**
     * 缓存的排行列表
     */
    List<GuildRank> cachedRankList;
    /**
     * 上次加载时间
     */
    long lastLoadTime;

    CrossRankMessageModule messageModule;

    public GuildCrossRankLoader() {

    }

    public GuildCrossRankLoader(CrossRankMessageModule messageModule, int rankId) {
        this.messageModule = messageModule;
        this.rankId = rankId;
    }

    private CrossRankMessageModule getMessageModule() {
        return messageModule;
    }

    public void updateGuild(GameBody game, RoleSnapshot roleSnapshot, GuildBasicPO guildBasic) {
        getMessageModule().updateGuildRecord(game, guildBasic.uid, guildBasic.guildName, roleSnapshot, rankId, guildBasic);
    }

    /**
     * 发送跨服排行数据
     * @param limit 限制
     */
    public GameResult<Void> sendCrossRankList(Role role, int limit) {
        GameResult<Void> r = new GameResult<>();
        String guildUid = role.getRes().guild.uid;
        long nowTime = System.currentTimeMillis();
        boolean forceLoad = CollectionUtils.isEmpty(cachedRankList)
                || limit > cachedRankList.size()
                || (StringUtils.isNotBlank(guildUid) && cachedRankList.stream().filter(Objects::nonNull).anyMatch(rank -> StringUtils.equals(guildUid, rank.owner.uid)))
                || nowTime - lastLoadTime >= GDRank.CROSS_RANK_CACHE_TIME_MILLIS;
        CrossRankMessageModule.CrossRankGuildGetListResp res = getMessageModule()
                .getGuildRankList(role.getGame(), guildUid, limit, forceLoad);
        if (!res.ok()) {
            return r.fail(res.ec, res.em);
        }
        if (res.list != null) {
            cachedRankList = res.list;
            cachedRankList.removeIf(e -> e == null || e.owner == null);
            lastLoadTime = nowTime;
        }
        List<GuildRank> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(cachedRankList)) {
            if (limit >= cachedRankList.size()) {
                list.addAll(cachedRankList);
            } else {
                list.addAll(cachedRankList.subList(0, limit));
            }
        }
        GuildRank self = res.self;
        if (self == null) {
            GuildPO guild = role.getRes().guild.getGuild();
            if (guild != null) {
                self = new GuildRank(guild);
            }
        }
        role.sendNow(new GuildRankDataResp(res.myRank, self, list, rankId, 0));
        return r.success();
    }
}
